/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.policies.pool;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;
import org.fireflow.pdl.fpdl20.diagram.Fpdl20DiagramElement;
import org.fireflow2.designer.eclipse.commands.lane.CreateLaneCommand;
import org.fireflow2.designer.eclipse.modelwrapper.ModelWrapper;
import org.fireflow2.designer.eclipse.palette.DiagramElementSubType;
import org.fireflow2.designer.eclipse.policies.CommonResizableEditPolicy;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class PoolEditPolicy extends org.eclipse.gef.editpolicies.FlowLayoutEditPolicy{
	protected Command getCloneCommand(ChangeBoundsRequest request) {
//		CloneCommand clone = new CloneCommand();
//		clone.setParent((LogicDiagram)getHost().getModel());
//		
//		EditPart after = getInsertionReference(request);
//		int index = getHost().getChildren().indexOf(after);
//		
//		Iterator i = request.getEditParts().iterator();
//		GraphicalEditPart currPart = null;
//		
//		while (i.hasNext()) {
//			currPart = (GraphicalEditPart)i.next();
//			clone.addPart((LogicSubpart)currPart.getModel(), index++);
//		}
//		
//		return clone;
		return null;
	}
		
	protected Command createAddCommand(EditPart child, EditPart after) {
//		AddCommand command = new AddCommand();
//		command.setChild((LogicSubpart)child.getModel());
//		command.setParent((LogicFlowContainer)getHost().getModel());
//		int index = getHost().getChildren().indexOf(after);
//		command.setIndex(index);
//		return command;
		return null;
	}

//	/**
//	 * @see org.eclipse.gef.editpolicies.LayoutEditPolicy#createChildEditPolicy(org.eclipse.gef.EditPart)
//	 */
	protected EditPolicy createChildEditPolicy(EditPart child) {
//		LogicResizableEditPolicy policy = new LogicResizableEditPolicy();
//		policy.setResizeDirections(0);
		CommonResizableEditPolicy policy = new CommonResizableEditPolicy();
		return policy;
	}

	protected Command createMoveChildCommand(EditPart child, EditPart after) {
//		LogicSubpart childModel = (LogicSubpart)child.getModel();
//		LogicDiagram parentModel = (LogicDiagram)getHost().getModel();
//		int oldIndex = getHost().getChildren().indexOf(child);
//		int newIndex = getHost().getChildren().indexOf(after);
//		if (newIndex > oldIndex)
//			newIndex--;
//		ReorderPartCommand command = new ReorderPartCommand(childModel, parentModel, newIndex);
//		return command;
		return null;
	}

	protected Command getCreateCommand(CreateRequest request) {
    	Fpdl20DiagramElement newObj = (Fpdl20DiagramElement) request.getNewObject();

    	if (!newObj.equals(Fpdl20DiagramElement.LANE)){
    		return null;
    	}
    	
    	AbstractGraphicalEditPart poolEditPart = (AbstractGraphicalEditPart)this.getHost();

    	
		CreateLaneCommand command = new CreateLaneCommand();
		command.setClientArea(poolEditPart.getContentPane().getClientArea());
		ModelWrapper parent = (ModelWrapper)this.getHost().getModel();
		command.setParentWrapper(parent);
		command.setNewDiagramElement(newObj);
		DiagramElementSubType subType = (DiagramElementSubType)request.getNewObjectType();
		command.setDiagramElementSubType(subType);
		
		EditPart after = getInsertionReference(request);
		int index = getHost().getChildren().indexOf(after);
		command.setIndex(index);
		return command;
	}
}
